package com.wzl.customviewproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wzl.customviewproject.R;
import com.wzl.customviewproject.interfaces.OnItemClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：longxiansen
 * 功能：
 */
public class XiaoTypeAdapter extends RecyclerView.Adapter<XiaoTypeAdapter.XiaoMiViewHolder> {
    private Context context;
    private List<String> datas = new ArrayList<>();
    private OnItemClickListener onItemClickListener;
    private int selectedPosition = 0;
    public XiaoTypeAdapter(Context context, List<String> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setCurrentItem(int position){
        selectedPosition = position;
        notifyDataSetChanged();
    }

    @NonNull
    @NotNull
    @Override
    public XiaoMiViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new XiaoMiViewHolder(LayoutInflater.from(context).inflate(R.layout.module_list_item_xiaomi_type,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull XiaoTypeAdapter.XiaoMiViewHolder holder, int position) {
        if (selectedPosition == position) {
            holder.selectedFlag.setVisibility(View.VISIBLE);
            holder.tvContent.getPaint().setFakeBoldText(true);
            holder.tvContent.setTextSize(16);
        }else {
            holder.selectedFlag.setVisibility(View.GONE);
            holder.tvContent.getPaint().setFakeBoldText(false);
            holder.tvContent.setTextSize(14);
        }
        holder.tvContent.setText(datas.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = position;
                onItemClickListener.setOnItemClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class XiaoMiViewHolder extends RecyclerView.ViewHolder{
        TextView tvContent;
        TextView selectedFlag;
        public XiaoMiViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.item_tv);
            selectedFlag = itemView.findViewById(R.id.flag);
        }
    }
}
